<?php
/**
 * [LupMisNotAllowedSync]
 * 本代码为系统自动生成代码，请根据自己业务进行修改;
 * 生成时间 2022-02-18 18:16:27;
 * 版权所有 2020-2022 lizhongwen，并保留所有权利;
 * 说明: 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；不允许对程序代码以任何形式任何目的的再发布;
 * 作者: 中文Lee;
 * 作者主页: http://www.lizhongwen.com;
 * 邮箱: 360811363@qq.com;
 * QQ: 360811363;
 */
namespace modules\sys\service\impl;

use application\core\Result;
use application\filter\SysFilter;
use application\core\RedisUtil;
use application\core\Request;
use modules\sys\component\GetCacheComponent;
use modules\sys\component\SetCacheComponent;
use modules\sys\mapper\AccountTokenMapper;
use modules\sys\mapper\ActionMapper;
use modules\sys\mapper\PageMapper;
use modules\sys\component\TreeComponent;
use modules\sys\mapper\RolePageActionMapper;
use modules\sys\mapper\RoleTreeMapper;
use modules\sys\service\InitService;

class InitServiceImpl implements InitService
{

    public function cache(): array
    {
        if(!REDIS_ENABLE){
            return Result::fail(false,[],"REDIS未开启！");
        }
        SetCacheComponent::table();
        SetCacheComponent::tree();
        SetCacheComponent::page();
        SetCacheComponent::action();
        SetCacheComponent::role();
        SetCacheComponent::field();
        SetCacheComponent::dict();
        SetCacheComponent::dictData();
        SetCacheComponent::config();
        SetCacheComponent::catalog();
        return Result::success(false,[],"缓存更新成功");
    }
    public function init(): array
    {
        $accessToken = Request::getHeader('accesstoken');
        $accessuid = Request::getHeader('accessuid');
        // 根据access_token 获取账号信息

        if (REDIS_ENABLE) {
            $redis = RedisUtil::instance();
            $accountRs = $redis->hGet("sys_account_token", $accessuid);
            $accountRs = json_decode($accountRs, true);
        } else {
            $accountRs = AccountTokenMapper::queryByAccountId([
                'account_id' => $accessuid
            ]);
        }
        $configRs = GetCacheComponent::config();
        $data = [
            'home_info' => [
                'title' => '控制台',
                'href' => 'pages/welcome.html'
            ],
            'system_version' => $configRs['system_version'],
            'logo_info' => [
                'image' => $configRs['logo'],
                'href' => '',
                'title' => $configRs['system_name']
            ],
            'left_tree_show' => $configRs['left_tree_show'],
            'account_name' => $accountRs['account_name'],
            'accesstoken' => $accessToken,
            'menu_info' => [],
            'dict' => []
        ];

        $roleTreeRs = RoleTreeMapper::listsByRoleIdsIn(['role_ids' => SysFilter::$roleIds]);
        $treeIdsArr = [];
        foreach ($roleTreeRs as $v){
            $treeIdsArr[] = $v['tree_id'];
        }

        $treeList = (new TreeComponent())->menuTree($treeIdsArr,true);
        //如果一级菜单下没有子菜单这移除该菜单
        foreach ($treeList as $k=>$v){
            if(empty($v['child'])){
                unset($treeList[$k]);
            }
        }
        $data['menu_info'] = array_values($treeList);
        $dictData = [];
        // 查询数据字典数据
        $dictDataRs = GetCacheComponent::dictData();
        foreach ($dictDataRs as $k => $v) {
            $dictData[$k]['dict_label'] = $v['dict_label'];
            $dictData[$k]['dict_code'] = $v['dict_code'];
            $dictData[$k]['dict_value'] = $v['dict_value'];
            $dictData[$k]['is_default'] = intval($v['is_default']);
            $dictData[$k]['css_style'] = $v['css_style'];
        }
        $data['dict'] = $dictData;
        return $data;
    }

    public function loginInfo(): array
    {
        return Result::success(false,GetCacheComponent::config());
    }

    public function welcome(): array
    {
        $ret['config'] = GetCacheComponent::config();
        $ret['system'] = [
            'server_info' => $_SERVER['SERVER_SOFTWARE'],
            'server_port' => $_SERVER['SERVER_PORT'],
            'php_version' => PHP_VERSION,
            'server_ip' => GetHostByName($_SERVER['SERVER_NAME']),
            'os' => php_uname(),
            'server_date_time' => date("Y-m-d H:i:s")
        ];
        return Result::success(false,$ret);
    }

    public function getPageAction($code): array
    {
        $pageRs = PageMapper::queryByCode([
            'code' => $code
        ]);
        $pageActionRs = RolePageActionMapper::listsByRoleIdsIn(['role_ids' => SysFilter::$roleIds]);
        $actionRs = ActionMapper::listsByPageId([
            'page_id' => $pageRs['id']
        ]);
        $actionIdsArr = [];
        foreach ($pageActionRs as $v){
            if($v['page_id']==$pageRs['id']){
                $_actionIds= $v['action_ids'];
                $_arr = explode(",",$_actionIds);
                foreach ($_arr as $_actionId){
                    $actionIdsArr[] = $_actionId;
                }
            }
        }
        $actionArr = [] ;
        foreach ($actionRs as $v){
            if(in_array($v['id'],$actionIdsArr)){
                $actionArr[] = $v;
            }
        }
        return Result::success(false,[
            'page' => $pageRs,
            'action' => $actionArr
        ]);
    }
}

